package com.gdut.simple.presenter;

import com.gdut.simple.R;
import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.NetworkUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.HotPickContract;
import com.gdut.simple.data.entity.News;
import com.gdut.simple.model.IHotPickModel;
import com.gdut.simple.model.impl.HotPickModel;

import java.util.List;

public class HotPickPresenter implements HotPickContract.Presenter {


    private HotPickContract.View mHotPickView;
    private IHotPickModel mHotPickModel;

    public HotPickPresenter(HotPickContract.View view) {
        this.mHotPickView = view;
        mHotPickModel = new HotPickModel();
        mHotPickView.setPresenter(this);
    }

    @Override
    public void refresh() {
        mHotPickView.showRefreshProgress();
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mHotPickView.hideRefreshProgress();
            mHotPickView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        mHotPickModel.refresh(10, new RequestCallback<List<News>>() {
            @Override
            public void onSuccess(List<News> list) {
                mHotPickView.getItemList().clear();
                mHotPickView.getItemList().addAll(list);
                mHotPickView.refreshList();
                mHotPickView.reset();
                mHotPickView.hideRefreshProgress();
            }

            @Override
            public void onFailure(String message) {
                mHotPickView.hideRefreshProgress();
                mHotPickView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_refresh_fail));
            }
        });
    }

    @Override
    public void loadMore(int page) {
        if (!NetworkUtils.isConnected(BasicApplication.getApplication())) {
            mHotPickView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_error_net));
            return;
        }
        mHotPickModel.loadMore(10, page, new RequestCallback<List<News>>() {
            @Override
            public void onSuccess(List<News> list) {
                if (list == null || list.size() == 0) {
                    return;
                }
                mHotPickView.getItemList().addAll(list);
                mHotPickView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mHotPickView.showMessage(BasicApplication.getApplication().getResources().getString(R.string.msg_load_fail));
            }
        });
    }

}
